package com.meatball.game.websocket

import com.meatball.game.service.AiGameService
import com.meatball.game.service.RoomCacheService
import com.meatball.game.websocket.interceptor.WebsocketHandshakeInterceptor
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.web.socket.config.annotation.EnableWebSocket
import org.springframework.web.socket.config.annotation.WebSocketConfigurer
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry

/**
 * TODO websocket配置类
 *
 * @author 张翔宇
 * @since 2024年3月8日
 */
@Configuration
@EnableWebSocket
class WebSocketConfig (
        private val aiGameService: AiGameService,
        private val stringRedisTemplate: StringRedisTemplate,
        private val roomCacheService: RoomCacheService
) : WebSocketConfigurer {
    override fun registerWebSocketHandlers(registry: WebSocketHandlerRegistry) {
        registry.addHandler(WebsocketHandler(aiGameService, stringRedisTemplate, roomCacheService),"/room")
                .addInterceptors(WebsocketHandshakeInterceptor())
                .setAllowedOrigins("*")
    }
}